// Display and hide scripts for BoundLookup, PopupButton and TemplatePopupButton controls.
function Biz_BoundLookup_Display(sValId, sTextId, sDivId, sIframeId, sPopupUrlId, sSelectedValueQueryKey, sTextQueryKey, iPopupTopPx, iPopupLeftPx)
{
    var sPopupUrl = document.getElementById(sPopupUrlId).value;
    if (sPopupUrl == null  ||  sPopupUrl == "")
        return;
        
    // Dispay control.
    var ctlDiv = document.getElementById(sDivId);
    ctlDiv.style.zIndex = 100;
    ctlDiv.style.visibility = "visible";       
 
    // Build popup URL.
    var sConcatOp = (sPopupUrl.indexOf("?") >= 0) ? "&" : "?"
    if (sSelectedValueQueryKey != null  &&  sSelectedValueQueryKey != "")
    {
        var ctlSelectedValue = document.getElementById(sValId);
        var sClearedValue = Biz_BoundLookup_TrimString(ctlSelectedValue.value);
        if (sClearedValue != null  &&  sClearedValue != "")
        {
            sClearedValue = encodeURI(sClearedValue);
            sPopupUrl += sConcatOp + sSelectedValueQueryKey + "=" + sClearedValue;
            sConcatOp = "&";
        }
    }
 
    if (sTextQueryKey != null  &&  sTextQueryKey != "")
    {
        var ctlDisplayedValue = document.getElementById(sTextId);
        var sClearedValue = Biz_BoundLookup_TrimString(ctlDisplayedValue.value);
        if (sClearedValue != null  &&  sClearedValue != "")
        {
            sClearedValue = encodeURI(sClearedValue);
            sPopupUrl += sConcatOp + sTextQueryKey + "=" + sClearedValue;
            sConcatOp = "&";
        }
    }
    
    var ctlIframe = document.getElementById(sIframeId);
    ctlIframe.src = sPopupUrl;

    // Descroll.
    try
    {
        var iScrollY = (window.pageYOffset)?(window.pageYOffset):(document.documentElement)?document.documentElement.scrollTop:document.body.scrollTop;
        ctlDiv.style.top = iPopupTopPx + iScrollY + "px";
        var iScrollX = (window.pageXOffset)?(window.pageXOffset):(document.documentElement)?document.documentElement.scrollLeft:document.body.scrollLeft;
        ctlDiv.style.left = iPopupLeftPx + iScrollX + "px";
    }
    catch(e) 
    {
    }
}

function Biz_BoundLookup_Hide(sTextId, sDivId, sIframeId)
{
    var ctlDiv = document.getElementById(sDivId);
    ctlDiv.style.zIndex = -1;
    ctlDiv.style.visibility = "hidden";
    
    var ctlIframe = document.getElementById(sIframeId);
    ctlIframe.src = "";
    
    var ctlDisplayedValue = document.getElementById(sTextId);
    try
    {
        ctlDisplayedValue.focus();
    }
    catch(e) 
    {
    }
}

function Biz_BoundLookup_GetElementTopOffset(sElementId, bMinusScroll) 
{
    var ctl = document.getElementById(sElementId);
    if (ctl == null)
        return;
        
	var iTotalOffsetTop = 0;
	var ctlParent = ctl.offsetParent;
	if (ctlParent != null) 
	{
		iTotalOffsetTop = ctl.offsetTop;
		ctlParent = ctl.offsetParent;
		while (ctlParent != null) 
		{
			iTotalOffsetTop += ctlParent.offsetTop;
			ctlParent = ctlParent.offsetParent;
		}
	}
	
	if (bMinusScroll)
	{
	    var iScrollY = (window.pageYOffset)?(window.pageYOffset):(document.documentElement)?document.documentElement.scrollTop:document.body.scrollTop;
	    iTotalOffsetTop -= iScrollY;
	    if (iTotalOffsetTop < 0)
	        iTotalOffsetTop = 0;
	}
	
	return iTotalOffsetTop;
}

function Biz_BoundLookup_GetElementLeftOffset(sElementId, bMinusScroll) 
{
    var ctl = document.getElementById(sElementId);
    if (ctl == null)
        return;
        
	var iTotalOffsetLeft = 0;
	var ctlParent = ctl.offsetParent;
	if (ctlParent != null) 
	{
		iTotalOffsetLeft = ctl.offsetLeft;
		ctlParent = ctl.offsetParent;
		while (ctlParent != null) 
		{
			iTotalOffsetLeft += ctlParent.offsetLeft;
			ctlParent = ctlParent.offsetParent;
		}
	}
	
	if (bMinusScroll)
	{
	    var iScrollX = (window.pageXOffset)?(window.pageXOffset):(document.documentElement)?document.documentElement.scrollLeft:document.body.scrollLeft;
	    iTotalOffsetLeft -= iScrollX;
	    if (iTotalOffsetLeft < 0)
	        iTotalOffsetLeft = 0;
	}
	
	return iTotalOffsetLeft;
}

function Biz_BoundLookup_TrimString(s)
{
    return s.replace(/^\s+|\s+$/g,"");
}

// Auto popup/complete mode.
function Biz_BoundLookup_InitOldText(ctlText, sOldTextVariableName)
{
    if (null == Biz_BoundLookup_GetOldText(sOldTextVariableName))
        Biz_BoundLookup_SetOldText(sOldTextVariableName, ctlText.value);
}

function Biz_BoundLookup_GetOldText(sOldTextVariableName)
{         
    var sOldText;
    eval("sOldText = " + sOldTextVariableName + ";");
    return sOldText;
}

function Biz_BoundLookup_SetOldText(sOldTextVariableName, sValue)
{
    eval(sOldTextVariableName + " = sValue;");
}

function Biz_BoundLookup_AutoComplete(ctlText, sOldTextVariableName, sDisplayLookupFunctionName, sHideLookupFunctionName, iMinTextLen, sKeyTimeOutIdVariableName, iTimeoutDelay)
{
    var sClearTimeOutIfNotNull = "if (" + sKeyTimeOutIdVariableName + ") clearTimeout(" + sKeyTimeOutIdVariableName + ");";
    eval(sClearTimeOutIfNotNull);
    
    if (ctlText.value.length >= iMinTextLen)
    {
        var sDisplayIfTextChanged = "Biz_BoundLookup_DisplayIfTextChanged(\"" + ctlText.id + "\", \"" + sOldTextVariableName + "\", \"" + sDisplayLookupFunctionName + "\");";
        var sSetTimeout = sKeyTimeOutIdVariableName + " = setTimeout('" + sDisplayIfTextChanged + "', iTimeoutDelay);";
        eval(sSetTimeout);
    }
    else
    {
        eval(sHideLookupFunctionName + "();");
    }
}

function Biz_BoundLookup_DisplayIfTextChanged(sCtlTextId, sOldTextVariableName, sDisplayLookupFunctionName)
{
    var ctlText = document.getElementById(sCtlTextId);
    var sNewText = ctlText.value;
    var bHasChanged = (sNewText != Biz_BoundLookup_GetOldText(sOldTextVariableName));
    if (bHasChanged)
    {
        Biz_BoundLookup_SetOldText(sOldTextVariableName, sNewText);
        eval(sDisplayLookupFunctionName + "();");
    }
}
